package com.dongdongshop.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.dongdongshop.pojo.TbOrder;
import com.google.gson.JsonObject;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class OrderController {

    @Autowired
    private RocketMQTemplate rocketMqTemplate;

    /**
     *
     * @param orderId  订单id
     * @param trade_no 交易流水号
     * @param out_trade_no 商户订单号
     * @param status 支付状态
     * @param userId 用户id
     * @param money 支付金额
     * @return
     */
    @RequestMapping("text")
    @ResponseBody
    public String text(Long orderId,String trade_no,
                       String out_trade_no,String status,
                       String userId,String money) {
        //先发送半消息
        // 将要发送的消息转成json
        JSONObject json = new JSONObject();
        json.putOpt("userId",userId);
        json.putOpt("money",money);
        String jsonStr = json.toString();
        Message<String> message = MessageBuilder.withPayload(jsonStr).build();

        //将要修改的订单数据封装到实体类中
        TbOrder order = new TbOrder();
        order.setOrderId(orderId);
        order.setTardeNo(trade_no);
        order.setStatus(status);
        order.setOrderNum(out_trade_no);

        TransactionSendResult result = rocketMqTemplate.sendMessageInTransaction("tx-topic", message, order);

        System.out.println(JSONUtil.toJsonStr(result));

        return "ok";
    }
}
